#pragma once
#include <exception>

#include "../inc_windows_h.hpp"
#include "basic_allocator.hpp"

namespace wxx
{
	template<class T>
	struct local_allocator : basic_allocator<T>
	{
		WXX_memory_allocator_rebind(local_allocator);

		pointer allocate(size_type count) const
		{
			pointer ptr = reinterpret_cast<pointer>(
				::LocalAlloc(LPTR, sizeof(T) * count)
			);

			if (!ptr)
			{
				throw std::bad_alloc();
			}

			return ptr;
		}

		void deallocate(pointer ptr) const
		{
			if (ptr)
			{
				if (::LocalFree(reinterpret_cast<HLOCAL>(ptr)))
				{
					wxx_error::throw_last_win32();
				}
			}
		}

		void deallocate(pointer ptr, size_type) const
		{
			return deallocate(ptr);
		}
	};
};